<template>
  <a-modal title="配置" :width="800" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!--<div class="step-form-style-desc">
        <h3>说明</h3>
        <h4>品牌中心，礼品商城</h4><span></span>
        <p>限时时间段设置无效</p>
      </div>
      <a-divider />-->
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="小程序">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption" :disabled="isDisabled"
                    v-decorator="['customerId', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in editProps" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="门店">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption" :disabled="isDisabled"
                    v-decorator="['customerCode', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option v-for="item in stores" :key="item.code">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="收费模式">
          <a-select showSearch optionFilterProp="children" :filterOption="filterOption"
                    v-decorator="['payType', {rules: [{required: true, message: '必输'}]}]">
            <a-select-option  :value="2">收费</a-select-option>
            <a-select-option  :value='1'>不收费</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="游戏名称">
          <a-input v-decorator="[ 'gameName', {rules: []}]"></a-input>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="每天每人抽奖次数">
          <a-input v-decorator="[ 'oneDayCount', {rules: []}]"></a-input>
        </a-form-item>
<!--        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="转盘底色" extra="多个颜色用 “ | ” 区分，例如：#333|#666|#999999">-->
<!--          <a-input v-decorator="[ 'colors', {rules: []}]"></a-input>-->
<!--        </a-form-item>-->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="奖品配置">
          <div v-for="(item,i) in items">
            <a-select style="width: 35%" placeholder="请选择" v-model="item.productId">
              <a-select-option v-for="pw in couponList" :key="pw.id">{{pw.productName==null?pw.name:pw.productName}}</a-select-option>
            </a-select>
            <span style="margin-left: 5px">权重(比例)：</span>
            <a-input style="width: 10%" v-model="item.rate"/>&nbsp;

            <span style="margin-left: 1px">排序：</span>
              <a-input style="width: 10%" v-model="item.sortNo"/>&nbsp;
              <a-button style="width: 15%" type="danger" @click="onDel(i)">删除</a-button>
          </div>
          <a-button @click="onAdd" type="primary" style="margin-top: 6px">添加</a-button>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="抽奖二维码" extra="抽奖二维码">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="baseUrl + '/system/common/uploadFile'"
            :beforeUpload="beforeUpload"
            @change="uploadChange"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" width="300px"/>
            <div v-else>
              <a-icon :type="confirmLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">Upload</div>
            </div>
          </a-upload>
        </a-form-item>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment';
  import pick from 'lodash.pick'
  //import { selectAllByParentCode } from '@/api/mini/coupon'
  import { saveOrUpdate } from '@/api/game/game'
  import { optionselect } from '@/api/product/commodity'
  import { storeList } from '@/api/shop/account'
  //import { selectAllStore } from '@/api/mini/store'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      //图片相关
      baseUrl: process.env.VUE_APP_API_BASE_URL,
      imageUrl: '',
      imageUrl1: '',
      imageUrl2: '',
      bgImg: '',
      borderImg: '',
      buttonImg: '',
      couponList: [],
      items: [],
      stores:[],
      form: this.$form.createForm(this),
    }
  }

  const created = function() {
    storeList({}).then(res => {
      if (res.code === 200) {
        this.stores = res.data
      }
    })
  }

  export default {
    props: {
      editProps: {
        type: Array,
        default: null
      }
    },
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.storeCode = record.storeCode;
        this.isDisabled = true
        this.imageUrl = record.bgImg
        this.imageUrl1 = record.borderImg
        this.imageUrl2 = record.buttonImg
        this.items = this.items.concat(record.detailList)
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['customerId','colors','rate', 'oneDayCount','gameName','customerCode','payType'])
          setFieldsValue(formData)
        })
        const typeslist = '1,2,3,4'
        optionselect({types: typeslist,'storeCode':record.customerCode}).then(res => {
          if (res.code === 200) {
            this.couponList = res.data
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            values.bgImg = this.imageUrl
            values.borderImg = this.imageUrl1
            values.buttonImg = this.imageUrl2
            values.detailList = this.items
            values.type = 2;
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
        this.imageUrl = null
        this.imageUrl1 = null
        this.imageUrl2 = null
        this.bgImg = null
        this.borderImg = null
        this.buttonImg = null
        this.items = []
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      uploadChange(info) {
        if (info.file.status === 'uploading') {
          this.confirmLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.imageUrl = info.file.response.fileUrl;
          this.bgImg = info.file.response.filename
          this.confirmLoading = false;
        }
      },
      beforeUpload(file) {
        return true;
      },
      onAdd () {
        let cope = {}
        this.items.push(cope)
        // console.log(this.items);
      },
      onDel: function (index) {
        this.items.splice(index, 1)
      },
    },
  }
</script>

<style>
  .step-form-style-desc {
    padding: 0 56px;
    color: rgba(0, 0, 0, .45);
  }
  h3 {
    margin: 0 0 12px;
    color: rgba(0,0,0,.45);
    font-size: 16px;
    line-height: 32px;
  }

  h4 {
    margin: 0 0 4px;
    color: rgba(0,0,0,.45);
    font-size: 14px;
    line-height: 22px;
  }

  p {
    margin-top: 0;
    margin-bottom: 12px;
    line-height: 22px;
  }
</style>